<?php

namespace App\Admin\Controllers;

use App\Models\Article;
use App\Models\Comment;
use App\Models\OauthUser;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;

class CommentController extends AdminController
{

    public function index(Content $content)
    {
        return $content
            ->header(trans('admin.comment'))
            ->description(trans('admin.list'))
            ->body($this->grid()->render());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Comment());

        $grid->id('ID')->sortable();
        $grid->article()->title('文章');
        $grid->content('内容');
        $grid->user()->name('用户');
        $states = [
            'on' => ['value'=>1,'text' => '正常'],
            'off' => ['value'=>0,'text' => '无效'],
        ];
        $grid->status('状态')->switch($states);

        $grid->actions(function (Grid\Displayers\Actions $actions) {
            $actions->disableView();
        });

        $grid->tools(function (Grid\Tools $tools) {
            $tools->batch(function (Grid\Tools\BatchActions $actions) {
                $actions->disableDelete();
            });
        });
        $grid->filter(function($filter){
            $filter->column(1/2, function ($filter) {
                $filter->where(function ($query) {

                    $query->whereHas('user', function ($sub_query) {
                        $sub_query->where('name', 'like', "%{$this->input}%");
                    })->orWhere('content', 'like', "%{$this->input}%");

                }, '关键词')->placeholder('关键词(包括内容,用户名)');
                $filter->equal('article_id')->select(Article::all()->pluck('title', 'id'));
            });

            $filter->column(1/2, function ($filter) {
                $filter->gt('created_at')->datetime();
                $filter->between('updated_at')->datetime();
                $filter->equal('status')->radio([
                    1 => '正常',
                    0 => '关闭',
                ]);
            });

        });
        return $grid;
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header(trans('admin.comment'))
            ->description(trans('admin.create'))
            ->body($this->form());
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    public function form()
    {
        $Comment = new Comment();

        $form = new Form($Comment);

        $form->display('id', 'ID');

        $form->textarea('content', '内容')->rules('required');
        $form->select('oauth_user_id', '用户')->options(OauthUser::all()->pluck('name', 'id'))->rules('required');
        $form->select('article_id', '文章')->options(Article::all()->pluck('title', 'id'))->rules('required');

        $states = [
            'on'  => ['value' => 1, 'text' => '正常', 'color' => 'success'],
            'off' => ['value' => 0, 'text' => '无效', 'color' => 'danger'],
        ];
        $form->switch('status', '状态')->states($states);

        $form->display('created_at', trans('admin.created_at'));
        $form->display('updated_at', trans('admin.updated_at'));

        return $form;
    }

    /**
     * Edit interface.
     *
     * @param $id
     *
     * @return Content
     */
    public function edit($id, Content $content)
    {
        $this->is_add=false;
        return $content
            ->header(trans('admin.comment'))
            ->description(trans('admin.edit'))
            ->body($this->form()->edit($id));
    }

    /**
     * Show interface.
     *
     * @param mixed   $id
     * @param Content $content
     *
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header(trans('admin.comment'))
            ->description(trans('admin.detail'))
            ->body($this->detail($id));
    }

}
